﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.IO;

namespace FileMD5
{
    class Program
    {
        static string ToMd5(byte[] md5)
        {
            string OutString = "";
            for (int i = 0; i < md5.Length; i++)
            {
                OutString += md5[i].ToString("x2");
            }

            return OutString;
        }

        static void Main(string[] args)
        {
            if (args.Length != 2)
            {
                Console.WriteLine("请设置计算的资源路径！");
                Console.ReadKey();
                return;
            }

//             string filePath = "F:\\FTP Resource\\StreamingAsset"; // 文件目录
//             string saveFile = "F:\\FTP Resource\\fileslist.txt";
            string filePath = args[0]; // 文件目录
            string saveFile = args[1];

            StreamWriter Write = new StreamWriter(saveFile, false, Encoding.ASCII);
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();

            string[] allfiles = Directory.GetFiles(filePath, "*", SearchOption.AllDirectories);
            byte[] memcryBuf = new byte[1024*1024*512];
            foreach (string file in allfiles)
            {
                FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read);
                fileStream.Read(memcryBuf, 0, (int)fileStream.Length);
                byte[] md5value = md5.ComputeHash(memcryBuf, 0, (int)fileStream.Length);

                string newfile = file.Substring(filePath.Length + 1);
                Write.WriteLine("\"{0}\",\"{1}\"", newfile, ToMd5(md5value));
                Console.WriteLine("\"{0}\",\"{1}\"", newfile, ToMd5(md5value));
            }
            Write.Flush();
            Write.Close();
            Console.WriteLine("完成，共计文件:{0}个!", allfiles.Length);
            Console.ReadKey();
        }
    }
}
